package ext_biz

import (
	"fmt"
	"github.com/gogf/gf/encoding/gbinary"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/os/gfile"
	"github.com/gogf/gf/os/gtime"
	"github.com/gogf/gf/text/gstr"
	"github.com/skip2/go-qrcode"
)

func ClearLog() {
	log_keep_day := g.Cfg().GetInt("ext_biz.log_keep_day")
	expireDate := gtime.Now().AddDate(0, 0, -log_keep_day)
	g.Log().Line(true).Debug("ClearLog")
	path := "./log"
	if gfile.Exists(path) {
		files, err := gfile.DirNames(path)
		if err != nil {
			g.Log().Line(true).Error(err)
		} else {
			if len(files) > 0 {
				for _, f := range files {

					fArr := gstr.Split(f, ".")
					if len(fArr) == 2 {
						name := fArr[0]
						dt := gtime.NewFromStr(name)
						if dt.Before(expireDate) {
							g.Log().Line(true).Debugf("remove file:%s", path+"/"+f)
							err = gfile.Remove(path + "/" + f)
							if err != nil {
								g.Log().Line(true).Error(err)
							}
						}
					} else {
						g.Log().Line(true).Warningf("illegal file name:%s", f)
					}
				}
			} else {
				g.Log().Line().Debug("no log file!")
			}
		}
	}
}

func ClearDB() {
	db := g.DB(g.Cfg().GetString("custom.db_name"))

	record_keep_month := g.Cfg().GetInt("ext_biz.record_keep_month")
	expireDate := gtime.Now().AddDate(0, -record_keep_month, 0).Format("Y-m-d H:i")
	_, err := db.Exec(fmt.Sprintf(" DELETE FROM t_hzfgw_record WHERE rectime<'%s'", expireDate))
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		g.Log().Line(true).Debug("ClearDB t_hzfgw_record ok!")
	}

	status_his_keep_month := g.Cfg().GetInt("ext_biz.status_his_keep_month")
	expireDate = gtime.Now().AddDate(0, -status_his_keep_month, 0).Format("Y-m-d H:i")
	_, err = db.Exec(fmt.Sprintf(" DELETE FROM t_hzfgw_node_status_his WHERE date_time<'%s'", expireDate))
	if err != nil {
		g.Log().Line(true).Error(err)
	} else {
		g.Log().Line(true).Debug("ClearDB t_hzfgw_node_status_his ok!")
	}
}

func EncodeQrCode(sn uint32, pre string) (buff []byte) {
	//var png []byte
	//png, err := qrcode.Encode("https://example.org", qrcode.Low, 25)
	//if err != nil {
	//	g.Log().Line(true).Error()
	//} else {
	//	g.Log().Line(true).Debugf("buff % X,len=%d", png, len(png))
	//}

	//sn := 123
	content := fmt.Sprintf("%s_%08d", pre, sn)
	item, _ := qrcode.NewWithForcedVersion(content, 2, qrcode.Medium)
	item.DisableBorder = true
	//img := item.Image(25)
	//g.Log().Line().Debug(img)
	str := item.ToSmallString(false)
	fmt.Print(str)

	bitmap := item.Bitmap()
	xlen := len(bitmap)
	ylen := len(bitmap[0])
	g.Log().Line(true).Debugf("xlen=%d,ylen=%d", xlen, ylen)

	const bitmap_size = 25
	const bits_size = 625
	const bytes_size = 79

	var new_bitmap [bitmap_size][bitmap_size]int
	var bits_arr [bits_size]int
	//var bytes []byte

	idx := 0
	for x := 0; x < bitmap_size; x++ {
		for y := 0; y < bitmap_size; y++ {

			if bitmap[x][y] {
				new_bitmap[x][y] = 1
			} else {
				new_bitmap[x][y] = 0
			}

			bits_arr[idx] = new_bitmap[x][y]
			idx++

		}
	}
	g.Log().Line().Debug(new_bitmap)
	g.Log().Line().Debug(bits_arr)

	for i := 0; i < bits_size; i += 8 {
		if i < bits_size-1 {
			bits := [8]gbinary.Bit{gbinary.Bit(bits_arr[i]), gbinary.Bit(bits_arr[i+1]), gbinary.Bit(bits_arr[i+2]), gbinary.Bit(bits_arr[i+3]), gbinary.Bit(bits_arr[i+4]), gbinary.Bit(bits_arr[i+5]), gbinary.Bit(bits_arr[i+6]), gbinary.Bit(bits_arr[i+7])}
			val := gbinary.DecodeBitsToUint(bits[:])
			buff = append(buff, uint8(val))
		} else {
			//最后一个
			bits := [8]gbinary.Bit{gbinary.Bit(bits_arr[bits_size-1])}
			val := gbinary.DecodeBitsToUint(bits[:])
			buff = append(buff, uint8(val))
		}
	}
	g.Log().Line().Debugf("% X,len=%d", buff, len(buff))
	//buff = append(buff, bytes...)
	return buff
}
